<?php

/**
 * GeoForm class.
 * GeoForm is the data structure for sending and retrieving GeoData to Google
 */
class GeoForm extends CFormModel
{
	public $street;
	public $city;
	
/*
'prefix' is the start of the CC number as a string, any number of digits.
'length' is the length of the CC number to generate. Typically 13 or 16
*/

	private function completed_number($prefix, $length) {

    $ccnumber = $prefix;

    # generate digits

    while ( strlen($ccnumber) < ($length - 1) ) {
        $ccnumber .= rand(0,9);
    }

    # Calculate sum

    $sum = 0;
    $pos = 0;

    $reversedCCnumber = strrev( $ccnumber );

    while ( $pos < $length - 1 ) {

        $odd = $reversedCCnumber[ $pos ] * 2;
        if ( $odd > 9 ) {
            $odd -= 9;
        }

        $sum += $odd;

        if ( $pos != ($length - 2) ) {

            $sum += $reversedCCnumber[ $pos +1 ];
        }
        $pos += 2;
    }

    # Calculate check digit

    $checkdigit = (( floor($sum/10) + 1) * 10 - $sum) % 10;
    $ccnumber .= $checkdigit;

    return $ccnumber;
}

	public function CreditCardNumber($prefixList, $length=16, $howMany=1) {

    	for ($i = 0; $i < $howMany; $i++) {

        	$ccnumber = $prefixList[ array_rand($prefixList) ];
        	$result[] = $this->completed_number($ccnumber, $length);
    	}

    return $result;
	}
	


	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// username and password are required
			array('city, street', 'required'),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
			'city'=>'Ort',
			'street'=>'Strasse',
		);
	}
	
}
